local damage = require 'types.damage'
local random = math.pseudo_random

local mt = damage.__index 

mt.damage_type = '适应'

--护甲因子
mt.def_sub = 0.02
mt.attack_rate = 1


function get_ex_attack(source,types,data)
    local ad1,ap1 = source:get('物理固伤'), source:get('魔法固伤')
    local ad2,ap2 = source:get('物理伤害'), source:get('魔法伤害')
    local ad3,ap3 = source:get('物理固伤加成'), source:get('魔法固伤加成')
    ad1 = (ad1 + source:get('自适应固伤')) * (1 + (ad3 + source:get('自适应伤害') )*0.01)
    ap1 = (ap1 + source:get('自适应固伤')) * (1 + (ap3 + source:get('自适应伤害') )*0.01)
    if types=='物理' then
        return ad1,ad2
    elseif types=='魔法' then
        return ap1,ap2
    elseif types=='混合' then
        return (ad1 + ap1),(ad2+ap2)
    elseif types=='适应' then
        if ad2>ap2 then
            if data then
                data.damage_type = '物理'
            end
            return ad1,ad2
        else
            if data then
                data.damage_type = '魔法'
            end
            return ap1,ap2
        end
    end
    return 0,0
end


--[本地图技能伤害默认为攻击倍率]
--[特殊处理下数值]
function mt:get_base_damage()
    local source = self.source
    local damage = self.damage
    
    --伤害类型
    local hero = source.owner_unit or source
    local ad1,ad2 = get_ex_attack(hero,self.damage_type,self)
    damage = damage + ad1
    self.damage_add = ad2
    self['暴击'] = math.random(100)<=hero:get('暴击几率')
    self['暴击伤害'] = hero:get('暴击伤害') - 100
    return damage
end


function damage:damageCommon()
    local source = self.source
    local target = self.target
    local hero = source.owner_unit or source
    local skill = self.skill
    local crit = self['暴击'] and self['不暴击']==nil
    self.armor = target:get('护甲')
    if skill then
        skill:notify('hit_unit',source,target,self)
        if crit then
            skill:notify('on_crit',source,target,self)
        end
    end
    self:mul(self.damage_add)
    if crit then
        source:notify('单位-触发暴击',source,target,self)
        self:mul(self['暴击伤害'])
    end
    
    self:div(hero:get('伤害减免'))

    self:mul(hero:get('最终伤害'))
    self:mul(hero:get('伤害增幅'))
    
    if self:is_type('领主') then
        self:mul(hero:get('对BOSS增伤'))
    elseif self:is_type('挑战') then
        self:mul(hero:get('对挑战怪增伤'))
    elseif self:is_type('进攻') then
        self:mul(hero:get('对进攻怪增伤'))
    end
    --护甲减免
    self:div( 100*self.armor/(100 + self.armor))


end


function ac.unit.__index:get_test_dps()
    local attack_type = self:get_data('默认输出') or '普攻'
    local damage_type = self:get_data('默认伤害') or '物理'
    
    --伤害类型
    local ex,dmg1 = get_ex_attack(self,damage_type)
    
    --输出类型
    local atk1,atk2 = 0,0

    local damage = (self:get('攻击') + ex) * (1 + dmg1*0.01) * (1 + atk1*0.01) * (1 + atk2*0.01)
    local dps1 = damage * self:get('攻击速度')*0.01
    local dps2 = damage * self:get('冷却速度')*0.01
    return dps1,dps2
end

